package com.zsrt.bbu.bank.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zsrt.bbu.bank.dao.BankDao;
import com.zsrt.bbu.bank.entity.BankEntity;
import com.zsrt.bbu.bank.service.BankService;
import com.zsrt.bbu.card.dao.CardDao;
import com.zsrt.bbu.card.entity.CardEntity;

/**
 * 
 * @author zsrt
 *
 */


@Service
public class BankServiceImpl implements BankService {

	@Autowired
	private BankDao dao;
	
	@Autowired
	private CardDao cardDao;

	@Override
	public void findById(int id, Map<String, Object> mp) {
		
		try {
			BankEntity entity = dao.findById(id);
			mp.put("code", "200");
			mp.put("data", entity);
		} catch (Exception e) {
			mp.put("code", "500");
			e.printStackTrace();
		}
	}
	
	
	//spring你在我这个add上增加事务
	
	@Transactional(noRollbackFor={BadSqlGrammarException.class})
	@Override
	public void add(BankEntity entity, Map<String, Object> mp) {
		
			
			//1 保存银行
			dao.add(entity);
			
			
			
			
			//需求  增加一个银行的时候 必须 同时增加该银行下面的一张卡
			//2 保存银行卡  故意在插入到卡表的时候 报错
			CardEntity card  = new CardEntity();
			
			card.setCardNo("998877-06");
			
			
			//MySQLSyntaxErrorException spring只会自动回滚运行时异常  Exception
			cardDao.add(card);
			
			//int aa =2/0;  RuntimeException
			
			
			
			mp.put("code", "200");
			mp.put("data", entity);
	}

}
